package per.dg.retrofittestkotlin.project.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import per.dg.retrofittestkotlin.R
import per.dg.retrofittestkotlin.project.model.Article

//class HomeArticleListAdapter: PagedListAdapter<Article ,HomeArticleListViewHolder>(diffCallback) {
class HomeArticleListAdapter(
    var articleLis : MutableList<Article>?
): RecyclerView.Adapter<HomeArticleListViewHolder>() {

    companion object{
        private val diffCallback: DiffUtil.ItemCallback<Article> = object :
            DiffUtil.ItemCallback<Article>() {
            override fun areItemsTheSame(oldItem: Article, newItem: Article): Boolean {
                return oldItem.id == newItem.id
            }

            override fun areContentsTheSame(oldItem: Article, newItem: Article): Boolean {
                return oldItem.id == newItem.id
            }

        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeArticleListViewHolder {
        return HomeArticleListViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_rv_article_list, parent, false))
    }

    override fun onBindViewHolder(holder: HomeArticleListViewHolder, position: Int) {
//        holder.tv.setText(getItem(position)?.title)
        holder.tv.setText(articleLis?.get(position)?.title)
    }

    override fun getItemCount(): Int {
        if(articleLis == null) {
            return 0;
        }else{
            return articleLis!!.size
        }
    }

}

class HomeArticleListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
    var tv: TextView = itemView.findViewById(R.id.tv)
}